package com.unipay.common.result;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

/**
 * @Project: yitong_parent
 * @Author: C.yu
 * @Time: 2023-03-26 18:29:22
 * @Desc: TODO 全局统一返回结果类
 *      {
 *        "code": 200,
 *        "message": "成功",
 *        "data": [
 *           {
 *             "id": 2,
 *             "roleName": "系统管理员"
 *           }
 *        ],
 *        "ok": true
 *      }
 */
@ApiModel(value = "全局统一返回结果")
@Data
public class Result<T> {

    @ApiModelProperty(value = "返回状态码")
    private Integer code;
    @ApiModelProperty(value = "返回消息")
    private String message;
    @ApiModelProperty(value = "返回数据")
    private T data;

    public Result() {
    }


    // 0、返回数据
    protected static <T> Result<T> build(T data){
        Result<T> result = new Result<>();
        if (data != null){
            result.setData(data);
        }
        return result;
    }
    public static <T> Result<T> build(T body, Integer code, String message){
        Result<T> result = build(body);
        result.setCode(code);
        result.setMessage(message);
        return result;
    }
    public static <T> Result<T> build(T body, ResultCodeEnum resultCodeEnum){
        Result<T> result = build(body);
        result.setCode(resultCodeEnum.getCode());
        result.setMessage(resultCodeEnum.getMessage());
        return result;
    }

    // 2、操作成功
    public static <T> Result<T> ok(T data){
        //Result<T> result = build(data);
        return build(data, ResultCodeEnum.SUCCESS);
    }
    public static <T> Result<T> ok(){
        return Result.ok(null);
    }
    // 2、操作失败
    public static <T> Result<T> fail(T data){
        //Result<T> result = build(data);
        return build(data, ResultCodeEnum.FAIL);
    }
    public static <T> Result<T> fail(){
        return Result.fail(null);
    }

    // TODO ???
    public Result<T> message(String msg){
        this.setMessage(msg);
        return this;
    }
    public Result<T> code(Integer code){
        this.setCode(code);
        return this;
    }
}
